import { reactive } from 'vue'
import { getOptionList } from '@/api/base/brand'
import { getWarehouseOptionList } from '@/api/base/warehouse'
import { getProductOptionList, getProductDataForm } from '@/api/base/product'
import { addArrivalOrderData, getArrivalOrderDataForm, updateArrivalOrderData } from '@/api/warehouse/arrival-order'
export function useDetail(getFormValues: Function) {
  const status = reactive({
    saving: false,
    loading: false
  })

  const createOrUpdate = async () => {
    const values = getFormValues()
    if (status.saving) return false
    status.saving = true
    try {
      values?.code ? await updateArrivalOrderData(values.code, values) : await addArrivalOrderData(values)
      status.saving = false
      return true
    } catch (err) {
      status.saving = false
      return false
    }
  }

  // 根据编码获取产品详情
  const getProductByCode = async (code: string) => {
    return getProductDataForm(code)
  }

  // 获取产品下拉列表
  const getProductOptions = async () => {
    return getProductOptionList()
  }

  // 获取到货单详情
  const getDataById = async (userCode: any) => {
    return getArrivalOrderDataForm(userCode)
  }

  // 获取品牌方列表
  const getBrandOptions = async () => {
    return getOptionList()
  }

  // 获取仓库列表
  const getWarehouseList = async () => {
    return getWarehouseOptionList()
  }

  return {
    status,
    createOrUpdate,
    getDataById,
    getBrandOptions,
    getWarehouseList,
    getProductOptions,
    getProductByCode
  }
}
